<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;

use think\App;
use think\facade\View;
use think\response\Json;

class Message extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Message';

    //关联模型
    protected array $hasOne = ['user'];

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['title' => '昵称', 'templet' => '<div>{{d.user.nickname}}</div>'])
            ->addColumn(['title' => '编号', 'templet' => '<div>{{d.user.code}}</div>'])
            ->addColumn(['title' => '手机号', 'templet' => '<div>{{d.user.mobile}}</div>'])
            ->addColumn(['field' => 'title', 'title' => '标题'])
            ->addColumn(['field' => 'type', 'title' => '留言类型'])
            ->addColumn(['field' => 'comment', 'title' => '描述'])
            ->addColumn(['field' => 'create_time', 'title' => '时间'])
            ->addColumn(['field' => 'status', 'title' => '状态'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作'])
            ->addRightButtons(['edit' => ['title' => '回复/查看', 'size' => '30%']])
            ->addSearch([
                ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
                ['title__comment', '输入标题/描述', 'input', '', '', '', 'like']
            ])
            ->fetch();
    }

    public function edit($id = '', $tpl = 'message/edit', $info = ''): string
    {
        $info = $this->model::find($id);
        $list = $this->model::where('id', $id)->order('create_time', 'asc')->select();
        View::assign('list', $list);
        View::assign('id', $id);
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        $param['status'] = 2;
        $param['admin_id'] = $this->admin->id;
        $param['admin_name'] = $this->admin->admin_name;
        $param['reply_time'] = time();
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }


}
